package com.yanqu.road.server.manager.union.listener;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenRankMgr;
import com.yanqu.road.server.manager.union.huaben.rank.CrossGroupWeekUnionRankListModel;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.UnionHuaBenServerGroupMgr;

import java.util.List;
import java.util.Map;

public class CrossUnionHuaBenListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.JoinUnion.getValue()){
            if(unionInfo.isCross()){
                for (CrossUnionMember member : memberList) {
                    CrossUnionHuaBenConfigMgr.syncConfig(unionInfo, member);
                    CrossUnionHuaBenMgr.syncHuaBenData(unionInfo, member, null);
                    CrossUnionHuaBenMgr.syncMemberData(member.getServerId(), playerId);
                }
            }
        }

        if(syncType == eUnionSyncType.DeleteUnion.getValue()){
            if(unionInfo.isCross()){
                int weekId = CrossUnionHuaBenRankMgr.getThisWeekId();
                //话本榜单
                Map<Integer, Map<Integer, Map<Integer, CrossGroupWeekUnionRankListModel>>> weekMap = CrossUnionHuaBenRankMgr.getGroupWeekRankListModelMap();
                for (Map.Entry<Integer, Map<Integer, Map<Integer, CrossGroupWeekUnionRankListModel>>> weekEntry : weekMap.entrySet()) {
                    int groupId;
                    if(weekEntry.getKey() == weekId){
                        groupId = UnionHuaBenServerGroupMgr.getGroupIdByServerId(unionInfo.getServerId());
                    }else {
                        groupId = CrossUnionHuaBenRankMgr.getWeekServerGroup(weekId, unionInfo.getServerId());
                    }
                    for (Map.Entry<Integer, Map<Integer, CrossGroupWeekUnionRankListModel>> groupEntry : weekEntry.getValue().entrySet()) {
                        if(groupEntry.getKey() != groupId){
                            continue;
                        }
                        for (Map.Entry<Integer, CrossGroupWeekUnionRankListModel> degreeEntry : groupEntry.getValue().entrySet()) {
                            degreeEntry.getValue().removeRank(unionInfo.getUnionUid());
                        }
                    }
                }
            }
        }
    }
}
